<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Db;


class PutmoneyController extends AdminBaseController
{
    /**
     * 提现管理
     * @adminMenu(
     *     'name'   => '提现管理',
     *     'parent' => 'admin/Stock/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 30,
     *     'icon'   => '',
     *     'remark' => '提现管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param=$this->request->param();
        $pay_status=isset($param['pay_status']) ? $param['pay_status'] : 0;
        if($pay_status>0){
            $where['status']=$pay_status;
        }else{
            $where=null;
        }
        $put_balance_info=Db::name('put_balance')
            ->alias('a')
            ->field('a.*,user.mobile')
            ->join('user','user.id=a.uid')
            ->order('a.id desc')
            ->where($where)
            ->paginate('10');
        $put_balance_info->appends($param);
        $this->assign('pay_status', isset($param['pay_status']) ? $param['pay_status'] : 0);
        $this->assign('put_balance', $put_balance_info->items());
        $this->assign('pages',  $put_balance_info->render());
        return $this->fetch();
    }

    /**
     * 提现成功
     * @adminMenu(
     *     'name'   => '提现成功',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 30,
     *     'icon'   => '',
     *     'remark' => '提现成功',
     *     'param'  => ''
     * )
     */
    public function put_success(){
        $param=$this->request->param();
        $id=$param['id'];
        $put_info=Db::name('put_balance')->where('id',$id)->find();
        if($put_info['status']==0){
            $data['status']=1;
            $data['pay_time']=time();
            Db::name('put_balance')->where('id',$id)->update($data);
            $this->success('操作成功');
        }else{
            $this->error('该提现已操作，请刷新页面');
        }
    }

    /**
     * 提现失败
     * @adminMenu(
     *     'name'   => '提现失败',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 30,
     *     'icon'   => '',
     *     'remark' => '提现失败',
     *     'param'  => ''
     * )
     */
    public function put_error(){
        $param=$this->request->param();
        $id=$param['id'];
        $put_info=Db::name('put_balance')->where('id',$id)->find();
        $user_info=Db::name('user')->find($put_info['uid']);
        if($put_info['status']==0){
            Db::startTrans();
            try {
                $data['status']=2;
                $data['reason']=$param['refund_no_reason'];
                $data['pay_time']=time();
                Db::name('put_balance')->where('id',$id)->update($data);
                Db::name('user')->where('id',$put_info['uid'])->setInc('balance',$put_info['balance']);
                //添加记录
                $balance_info['user_id']=$put_info['uid'];
                $balance_info['create_time']=time();
                $balance_info['change']=$put_info['balance'];
                $balance_info['balance']=$user_info['balance']+$put_info['balance'];
                $balance_info['description']='用户提现失败退还';
                $balance_info['remark']=json_encode($put_info);
                Db::name('user_balance_log')->insert($balance_info);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
            }
            $this->error('操作成功');
        }else{
            $this->error('该提现已操作，请刷新页面');
        }
    }



}